<template>
    <han-status-bar />
    <view class="view-post">
        <!-- 头部导航 -->
        <my-post-view-header v-if="isCurrentUserPost" :userData="userData" />
        <user-post-view-header v-else :userData="userData" />

        <!-- 内容 -->
        <post-content
            v-if="postData.title.length > 0"
            :postData="postData"
        ></post-content>

        <!-- 评论 -->
        <post-comment-reply
            :postId="postData.postId"
            :authorId="userData.userId"
        ></post-comment-reply>

        <post-view-comment-bar></post-view-comment-bar>
    </view>
</template>

<script setup>
import myPostViewHeader from './components/myPostViewHeader.vue';
import userPostViewHeader from './components/userPostViewHeader.vue';
import postContent from './components/postContent.vue';
import postViewCommentBar from './components/postViewCommentBar.vue';
import postCommentReply from './components/postCommentReply.vue';

import { ref, onMounted, computed } from 'vue';
import { getPostFullInfoAPI } from '@/apis/postAPI';
import { useUserStore } from '@/stores/userStores';

onMounted(async () => {
    // 获取路由参数
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    postId.value = currentPage.options.postId;

    await userStore.init();

    // 获取帖子详情
    try {
        const res = await getPostFullInfoAPI(postId.value);
        postData.value = res.data.data;
        userData.value = postData.value.user;
    } catch (error) {
        console.error('获取帖子详情失败:', error);
    }
});

const userStore = useUserStore();
const postData = ref({
    imageUrls: [],
    title: '',
    content: '',
    location: '',
    user: {},
    createTime: '',
    postId: ''
});
const userData = ref({});
const postId = ref('');

// 判断是否是当前用户的帖子
const isCurrentUserPost = computed(() => {
    return userData.value.userId === userStore.currentUser.userId;
});
</script>

<style lang="scss">
.view-post {
}
</style>
